#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/types.h>
#include <stdlib.h> /* exit() */
#include <unistd.h> /* close() */
#include <string.h> /* memset() */

#define LOCAL_SERVER_PORT 40000
#define MAX_MSG 1024

void Die(char *mess) { perror(mess); exit(1); }

int main(int argc, char *argv[]) {
	int sock;
	struct sockaddr_in servAddr;
	char buffer[MAX_MSG];
	unsigned int echolen;
	int received = 0;

	if (argc != 4) {
		fprintf(stderr, "USAGE: %s <server_ip> <word> <port>\n", argv[0]);
		exit(1);
	}

  /* Create the UDP socket */
	if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
		Die("Failed to create socket");
	}
	/* Construct the server sockaddr_in structure */
	memset(&servAddr, 0, sizeof(servAddr));       /* Clear struct */
	servAddr.sin_family = AF_INET;                  /* Internet/IP */
	servAddr.sin_addr.s_addr = inet_addr(argv[1]);  /* IP address */
	servAddr.sin_port = htons(atoi(argv[3]));       /* server port */

  /* Send the word to the server */
	echolen = strlen(argv[2]);
	if (sendto(sock, argv[2], echolen, 0,
						 (struct sockaddr *) &servAddr,
						 sizeof(servAddr)) != echolen) {
		Die("Mismatch in number of sent bytes");
	}

	close(sock);
	exit(0);
}
